<template>
	<view class="mainpadding2">
		<u-navbar title="規格範例列表" placeholder autoBack>
			<view class="flexright xiaohui" slot="right" @click="jumpurl('/pages/workbench/guigefl')">添加</view>
		</u-navbar>
		<view class="mainpadding_top ffffff radius dingwei" v-for="item in list.data" :key="item.id"
			@click="checkitem(item)">
			<view class="checkfocus" v-if="item.ischeck">
				<u-icon name="checkbox-mark" color="#fff" size="16"></u-icon>
			</view>
			<view class="xiaohei titletext bold">{{item.name}}</view>
			<view class="flexright margin-top">
				<view class="flexleft margin_left" @click.stop="jumpdetail('/pages/workbench/guigefl?id=',item.id)">
					<u-icon name="edit-pen" color="#999" size="16"></u-icon>
					<view class="xiaohei">編輯</view>
				</view>
				<view class="flexleft margin_left" @click.stop="delitem(item.id)">
					<u-icon name="trash" color="#999" size="16"></u-icon>
					<view class="xiaohei">刪除</view>
				</view>
			</view>
		</view>
		<view class="posibottom mainpadding" v-if="type==1">
			<view class="submitbtn" @click="submit">確定</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				page: 1,
				limit: 12,
				list: {
					data: []
				},
				type: 0
			}
		},
		onLoad(options) {
			if (options.type) {
				this.type = options.type
			}
		},
		onShow() {
			this.init()
		},
		methods: {
			submit() {
				let dataList = JSON.parse(JSON.stringify(this.list.data))
				dataList.forEach(current => {
					let arr = current.content_text
					let arr1 = []
					arr.forEach(item => {
						arr1.push({
							images: item.image ? item.image.split(',') : [],
							image_arr: item.image_text ? item.image_text.split(',') : [],
							money: item.price,
							name: item.name,
						})
					})
					this.$set(current, "goods_spunat", arr1)
				})
				uni.$emit("checkggmb", dataList)
				uni.navigateBack(1)
			},
			checkitem(item) {
				if (this.type == 1) {
					item.ischeck = !item.ischeck
				}
			},
			delitem(id) {
				this.http.modal('提示', '確認刪除當前數據嗎？', true, (res) => {
					if (res) {
						this.http.request("/api/goods/delGgmb", 'GET', {
							id
						}).then(res => {
							if (res.code == 1) {
								this.http.toast("刪除成功")
								this.init()
							} else {
								this.http.toast(res.msg);
							}
						})

					}
				})
			},
			init(isPage, page) {
				let _this = this
				httpRequest.request('/api/goods/ggMbIndex', 'GET', {
					page: page || 1,
					limit: 12,
					shop_id: uni.getStorageSync("shop_id"),
				}).then(res => {
					uni.stopPullDownRefresh()
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (this.type == 1) {
							res.data.data.forEach(item => {
								this.$set(item, "ischeck", false)
							})
						}
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							this.list = res.data
							_this.list.data = resList.data;
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
		}
	}
</script>

<style>

</style>